package com.gj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gj.reggie.constant.LoginConstant;
import com.gj.reggie.entity.R;
import com.gj.reggie.entity.User;
import com.gj.reggie.service.UserService;
import com.gj.reggie.utils.SMSUtils;
import com.gj.reggie.utils.ValidateCodeUtils;
import com.sun.scenario.effect.impl.prism.PrReflectionPeer;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 郭嘉
 * @date 2023/1/5 - 17:18
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;



    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user,HttpSession session){
        //获取手机号
        String phone=user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            //生产随机地4位验证码
            String code=ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码为={}",code);
            //调用阿里云短信服务
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);
            //生产验证码保存到session
            //session.setAttribute(phone,code);


            //生产验证码保存到redis中，有效期为5分重
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("验证码短信发送成功");
        }
        return R.error("短信发送失败");
    }

    /**
     * 移动端用户登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
       //获取手机号
        String phone=map.get("phone").toString();
        //获取验证码
        String code=map.get("code").toString();
        //从session中获取数据
        //Object attribute=session.getAttribute(phone);

        //从redis中获取数据
        Object attribute=redisTemplate.opsForValue().get(phone);

        //验证码比对
        if(attribute!=null&&attribute.equals(code)){
            //成功登录成功
            LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone,phone);
            User user=userService.getOne(lambdaQueryWrapper);
            if(user==null){
                //判断当前手机号是否在用户表里，不在为新用户，放入User表里
                user=new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute(LoginConstant.LOGIN_USER,user.getId());

            //删除验证码
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return  R.error("登录失败");
    }
}
